package com.yzh.demo.design;

import com.alibaba.fastjson.JSON;
import com.yzh.design.demo.card.IQYCardService;
import com.yzh.design.demo.coupon.CouponResult;
import com.yzh.design.demo.coupon.CouponService;
import com.yzh.design.demo.goods.DeliverReq;
import com.yzh.design.demo.goods.GoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 模拟发奖服务
 */
public class PrizeController {
    private Logger logger = LoggerFactory.getLogger(PrizeController.class);

    public AwardRes awardToUser(AwardReq req) {
        String reqJson = JSON.toJSONString(req);
        AwardRes awardRes = null;
        try {
            logger.info("奖品发放开始{}。req：{}", req.getuId(), reqJson);
            //按照不同类型方法商品[1优惠卷、2实物商品、3兑换卡]
            if (req.getAwardType() == 1) {
                //创建优惠卷服务
                CouponService couponService = new CouponService();
                //调用service的发送优惠卷，然后接收返回结果
                CouponResult couponResult = couponService.sendCoupon(req.getuId(), req.getAwardNumber(), req.getBizId());
                //0000成功，0001发送失败
                if ("0000".equals(couponResult.getCode())) {
                    awardRes = new AwardRes("0000", "发放成功");
                }else {
                    awardRes = new AwardRes("0001", couponResult.getInfo());
                }
            } else if (req.getAwardType() == 2) {
                GoodsService goodsService = new GoodsService();
                DeliverReq deliverReq = new DeliverReq();
                deliverReq.setUserName(queryUserName(req.getuId()));
                deliverReq.setUserPhone(queryUserPhoneNumber(req.getuId()));
                deliverReq.setSku(req.getAwardNumber());
                deliverReq.setOrderId(req.getBizId());
                deliverReq.setConsigneeUserName(req.getExtMap().get("consigneeUserName"));
                deliverReq.setConsigneeUserPhone(req.getExtMap().get("consigneeUserPhone"));
                deliverReq.setConsigneeUserAddress(req.getExtMap().get("consigneeUserAddress"));
                Boolean isSuccess = goodsService.deliverGoods(deliverReq);
                //响应成功失败
                if (isSuccess) awardRes = new AwardRes("0000", "发放成功");
                else awardRes = new AwardRes("0001", "发放失败");
            } else if (req.getAwardType() == 3) {
                String bindMobilNumber = queryUserPhoneNumber(req.getuId());
                IQYCardService iqyCardService = new IQYCardService();
                iqyCardService.grantToken(bindMobilNumber, req.getAwardNumber());
                awardRes = new AwardRes("0000", "发放成功");
            }
            logger.info("奖品发放完成{}。", req.getuId());
        }catch (Exception e) {
            logger.info("奖品发放失败{}。req{}", req.getuId(), reqJson, e);
            awardRes = new AwardRes("0001", e.getMessage());
        }
        return awardRes;
    }
    private String queryUserName(String uId) {
        return "杨子豪";
    }
    private String queryUserPhoneNumber(String uId) {
        return "15663186666";
    }
}
